<?php
require_once __DIR__ . '/../vendor/autoload.php';

use \GuzzleHttp\Client;

$host    = 'http://localhost';
$request = new Client();
$uname   = 'apitest_uname_' . rand();
$pwd     = 'apitest_pwd_' . rand();

/**
 * 注册接口验证
 */
$request->post('POST', $host . '/user/register', [
    'form_params' =>
    [
        'uname' => $uname,
        'pwd'   => $pwd,
    ],
]);

echo $request->getStatusCode();
echo $request->getHeaderLine('content-type');

$rep = json_decode($request->getBody(), true);
if ($rep['errno'] !== 0) {
    die("Error: 注册用户失败，注册接口异常。错误信息:" . $rep['errmsg'] . "\n");
}
echo "注册用户接口测试成功，注册新用户：" . $uname . "\n";

/**
 * 登陆接口测试
 */
$request->post('POST', $host . '/userlogin?submit=1', [
    'form_params' => [
        'uname' => $uname,
        'pwd'   => $pwd,
    ],
]);

$rep = json_decode($request->getBody(), true);
if ($rep['errno'] !== 0) {
    die("Error: 登录失败，登录接口异常。错误信息：" . $rep['errmsg'] . "\n");
}
echo "登录接口测试成功，登录用户：" . $uname . ", 密码：" . $pwd . "\n";

echo 'check finish.' . "\n";
